package com.ysd.modules.app.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.modules.app.dao.ChapterVideoDao;
import com.ysd.modules.app.dao.StudentVideoDao;
import com.ysd.modules.app.entity.ChapterResult;
import com.ysd.modules.app.entity.ChapterVideoEntity;
import com.ysd.modules.app.entity.StudentVideoEntity;
import com.ysd.modules.app.entity.VideoResult;
import com.ysd.modules.app.service.ChapterVideoService;

@Service
public class ChapterVideoServiceImpl extends ServiceImpl<ChapterVideoDao, ChapterVideoEntity> implements ChapterVideoService{
	
	@Autowired
	private StudentVideoDao studentVideoDao;
	
	@Override
	public List<VideoResult> getCourseVideo(Integer stuId, Integer type) {
		List<VideoResult> rLsit = new ArrayList<VideoResult>();
		if(stuId != null) {
			List<StudentVideoEntity> selectList = studentVideoDao.selectList(new QueryWrapper<StudentVideoEntity>().eq("stu_id", stuId));
			for(StudentVideoEntity c : selectList) {
				Integer videoId = c.getVideoId();
				ChapterVideoEntity course = baseMapper.selectOne(new QueryWrapper<ChapterVideoEntity>().eq("id", videoId).eq("type", type));
				if(course != null) {
					VideoResult vr = new VideoResult();
					vr.setId(course.getChapterId());
					vr.setCourseName(course.getCourseName());
					vr.setOneFold(false);
					if(!rLsit.contains(vr)) {
						rLsit.add(vr);
					}
				}
			}
		}else {
			List<ChapterVideoEntity> selectList = baseMapper.selectList(null);
			for(ChapterVideoEntity v:selectList) {
				VideoResult vr = new VideoResult();
				vr.setId(v.getChapterId());
				vr.setCourseName(v.getCourseName());
				vr.setOneFold(false);
				if(!rLsit.contains(vr)) {
					rLsit.add(vr);
				}
			}
		}
		return rLsit;
	}
	
	@Override
	public List<ChapterResult> getChapterVideo(Integer cid, Integer type) {
		List<ChapterVideoEntity> selectList = baseMapper.selectList(new QueryWrapper<ChapterVideoEntity>().eq("course_id", cid).eq("type", type));
		List<ChapterResult> crList = new ArrayList<ChapterResult>();
		for(ChapterVideoEntity cv : selectList) {
			ChapterResult c = new ChapterResult();
			c.setId(cv.getChapterId());
			c.setChapterName(cv.getChapterName());
			c.setTwoFold(false);
			if(!crList.contains(c)) {
				crList.add(c);
			}
		}
		for(ChapterResult c : crList) {
			List<Map<String,Object>> video = getVideo(c.getId(), selectList);
			c.setMList(video);
		}
		return crList;
	}
	
	public List<Map<String, Object>> getVideo(Integer caid, List<ChapterVideoEntity> selectList){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for(ChapterVideoEntity c:selectList) {
			if(c.getChapterId() == caid) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("videoName", c.getVideoName());
				map.put("listStart", false);
				map.put("duration", c.getDuration());
				map.put("url", c.getUrl());
				list.add(map);
			}
		}
		return list;
	}

}
